<script setup>
import {onMounted, reactive, ref} from "vue";
import axios from 'axios'
import {useRouter} from "vue-router";

const router = useRouter()

const httpRequest = axios.create({
  baseURL: '/api',
  headers: {
    'Content-Type': 'application/json',
    'Token': 'eyJhbGciOiJIUzI1NiJ9.eyJlbXBJZCI6MSwiZXhwIjoxNzA4MzE0NjAyfQ.Peb3RUgWdDce1gmZA8_G_ln4T-zyvA-zIGNmoyMWrkw'
  }
})

onMounted(() => {
  remoteAdminSetmealPage()
})

const searchForm = reactive({
  name: undefined,
  categoryId: undefined,
  status: undefined
})

const categoryList = ref([
  {name: '人气套餐', id: 1},
  {name: '精选套餐', id: 2}
])

const tableData = ref([])

const remoteAdminSetmealPage = () => {
  httpRequest.get('/admin/setmeal/page', {
    params: {
      page: 1,
      pageSize: 10,
      categoryId: searchForm.categoryId,
      name: searchForm.name,
      status: searchForm.status
    }
  }).then(({data}) => {
    tableData.value = data.data.records
  })
}

const search = () => {
  remoteAdminSetmealPage()
}
// todo 新增按钮事件
const onCreateHandle = () => {
  router.push({
    path: '/setmeal-add'
  })
}
</script>

<template>
  <!-- todo 编写页面代码 -->
  <el-card>
    <el-form inline>
      <el-form-item label="套餐名称">
        <el-input v-model="searchForm.name" placeholder="套餐名称"/>
      </el-form-item>
      <el-form-item label="套餐分类">
        <el-select v-model="searchForm.categoryId" class="w160" placeholder="套餐分类">
          <el-option v-for="(category, index) in categoryList" :label="category.name" :value="category.id"
                     :key="index"/>
        </el-select>
      </el-form-item>
      <el-form-item label="售卖状态">
        <el-select  v-model="searchForm.status" class="w160" placeholder="售卖状态">
          <el-option label="停售" :value="0"/>
          <el-option label="起售" :value="1"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="info" @click="search">查询</el-button>
        <el-button type="primary" @click="onCreateHandle">新增</el-button>
      </el-form-item>
    </el-form>

    <!--  数据表格  -->
    <el-table :data="tableData">
      <el-table-column label="套餐名称" prop="name"></el-table-column>
      <el-table-column label="图片" prop="image">
        <template #default="scope">
          <el-image :src="scope.row.image"></el-image>
        </template>
      </el-table-column>
      <el-table-column label="套餐分类" prop="categoryName"></el-table-column>
      <el-table-column label="套餐价" prop="price"></el-table-column>
      <el-table-column label="售卖状态" prop="status"></el-table-column>
      <el-table-column label="最后操作时间" prop="updateTime"></el-table-column>
      <el-table-column label="操作">
        <template #default="scope">
          <el-button link type="primary">修改</el-button>
          <el-button link type="danger">删除</el-button>
          <el-button link type="primary">起售</el-button>
          <el-button link type="primary">停售</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
</template>

<style scoped>

</style>
